<template>
  <div class="auth-container" v-loading="isSubmitting">
    <div class="auth-card relative">
      <Logo class="absolute top-6 left-6" size="small" />

      <div class="auth-header mt-3">
        <h1 class="auth-title">注册</h1>
        <p class="auth-subtitle">注册您的账号，开始使用全部功能</p>
      </div>

      <form @submit.prevent="handleRegister" class="auth-form">
        <div class="form-group">
          <label for="username" class="form-label">用户名</label>
          <div class="input-wrapper">
            <span class="input-icon">
              <div class="i-carbon-user text-lg"></div>
            </span>
            <input id="username" v-model="form.username" class="form-input pl-10" placeholder="请输入用户名" maxlength="20" required />
          </div>
        </div>

        <div class="form-group">
          <label for="password" class="form-label">密码</label>
          <div class="input-wrapper">
            <span class="input-icon">
              <div class="i-carbon-password text-lg"></div>
            </span>
            <input
              id="password"
              type="password"
              v-model="form.password"
              class="form-input pl-10"
              placeholder="请输入密码"
              maxlength="50"
              required
            />
          </div>
          <p class="input-hint">密码长度不少于6位</p>
        </div>

        <div class="form-group">
          <label for="confirmPassword" class="form-label">确认密码</label>
          <div class="input-wrapper">
            <span class="input-icon">
              <div class="i-carbon-password text-lg"></div>
            </span>
            <input
              id="confirmPassword"
              type="password"
              v-model="form.confirmPassword"
              class="form-input pl-10"
              placeholder="请再次输入密码"
              maxlength="50"
              required
            />
          </div>
        </div>

        <div class="form-group">
          <label for="email" class="form-label">电子邮箱（选填）</label>
          <div class="input-wrapper">
            <span class="input-icon">
              <div class="i-carbon-email text-lg"></div>
            </span>
            <input id="email" type="email" v-model="form.email" class="form-input pl-10" placeholder="用于找回密码" />
          </div>
        </div>

        <div class="form-agreement">
          <label class="agreement-checkbox">
            <input type="checkbox" v-model="agreeTerms" required />
            <span>我已阅读并同意</span>
          </label>
          <NuxtLink to="/user-agreement" class="auth-link">用户协议</NuxtLink>
          <span>和</span>
          <NuxtLink to="/privacy-policy" class="auth-link">隐私政策</NuxtLink>
        </div>

        <button
          type="submit"
          class="auth-btn-primary"
          :disabled="isSubmitting || !agreeTerms"
          :class="{ 'opacity-60 cursor-not-allowed': isSubmitting || !agreeTerms }"
        >
          <span v-if="isSubmitting" class="i-carbon-renew animate-spin mr-2"></span>
          {{ isSubmitting ? '注册中...' : '注册' }}
        </button>
      </form>

      <div class="auth-footer">
        <p>已有账号？<NuxtLink to="/login" class="auth-link">立即登录</NuxtLink></p>
      </div>
    </div>

    <div class="auth-decoration">
      <div class="auth-decoration-circle circle-1"></div>
      <div class="auth-decoration-circle circle-2"></div>
      <div class="auth-decoration-circle circle-3"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus';
import { queryRegister } from '~/apis/auth';

const userStore = useUserStore();
const form = reactive({ username: '', password: '', confirmPassword: '', email: '' });
const agreeTerms = ref(false);
const isSubmitting = ref(false);

const handleRegister = async () => {
  if (form.password !== form.confirmPassword) return ElMessage.error('两次输入的密码不一致');
  if (form.password.length < 6) return ElMessage.error('密码长度不能少于6位');
  isSubmitting.value = true;
  const res = await queryRegister({ ...form, email: form.email || undefined }).finally(() => (isSubmitting.value = false));
  userStore.onRegister(res.data);
  navigateTo('/');
};

definePageMeta({ layout: 'auth' });

onMounted(() => {
  if (userStore.isLoggedIn) navigateTo('/');
});
</script>

<style scoped>
.auth-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: linear-gradient(135deg, #f5f3ff 0%, #e0e7ff 100%);
  position: relative;
  overflow: hidden;
}

.auth-card {
  background: white;
  width: 100%;
  max-width: 420px;
  border-radius: 1.5rem;
  box-shadow: 0 10px 25px rgba(107, 70, 193, 0.1);
  padding: 2.5rem;
  position: relative;
  z-index: 10;
}

.auth-header {
  text-align: center;
  margin-bottom: 2rem;
}

.auth-title {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--primary-purple, #6b46c1);
  margin-bottom: 0.5rem;
}

.auth-subtitle {
  color: var(--text-secondary, #64748b);
  font-size: 0.95rem;
}

.auth-form {
  margin-bottom: 1.5rem;
}

.form-group {
  margin-bottom: 1.25rem;
}

.form-label {
  display: block;
  font-weight: 500;
  margin-bottom: 0.5rem;
  color: var(--text-primary, #1e293b);
  font-size: 0.9rem;
}

.input-wrapper {
  position: relative;
}

.input-icon {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
}

.form-input {
  display: block;
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  line-height: 1.5;
  color: #1e293b;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #e2e8f0;
  border-radius: 0.75rem;
  transition:
    border-color 0.15s ease-in-out,
    box-shadow 0.15s ease-in-out;
}

.form-input:focus {
  border-color: #a78bfa;
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.2);
  outline: none;
}

.input-hint {
  margin-top: 0.3rem;
  font-size: 0.75rem;
  color: #94a3b8;
}

.form-agreement {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem;
  margin-bottom: 1.5rem;
  font-size: 0.875rem;
  color: #64748b;
}

.agreement-checkbox {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.auth-link {
  color: var(--primary-purple, #6b46c1);
  font-weight: 500;
  text-decoration: none;
  transition: color 0.2s;
}

.auth-link:hover {
  color: var(--secondary-purple, #805ad5);
  text-decoration: underline;
}

.auth-btn-primary {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: center;
  background-color: var(--primary-purple, #6b46c1);
  color: white;
  font-weight: 600;
  padding: 0.75rem;
  border-radius: 9999px;
  transition: all 0.2s;
  box-shadow: 0 4px 6px rgba(107, 70, 193, 0.2);
  margin-bottom: 1rem;
}

.auth-btn-primary:hover:not(:disabled) {
  background-color: var(--secondary-purple, #805ad5);
  transform: translateY(-1px);
  box-shadow: 0 6px 8px rgba(107, 70, 193, 0.3);
}

.auth-footer {
  text-align: center;
  color: #64748b;
  font-size: 0.875rem;
}

.auth-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.auth-decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, #9f7aea 0%, #7f9cf5 100%);
  opacity: 0.1;
  animation-duration: 15s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  right: -50px;
  animation-name: float-1;
}

.circle-2 {
  width: 200px;
  height: 200px;
  bottom: -50px;
  left: -50px;
  animation-name: float-2;
}

.circle-3 {
  width: 100px;
  height: 100px;
  top: 40%;
  right: 10%;
  animation-name: float-3;
}

@keyframes float-1 {
  0%,
  100% {
    transform: translate(0, 0) rotate(0deg);
  }
  50% {
    transform: translate(-30px, 30px) rotate(5deg);
  }
}

@keyframes float-2 {
  0%,
  100% {
    transform: translate(0, 0) rotate(0deg);
  }
  50% {
    transform: translate(30px, -20px) rotate(-5deg);
  }
}

@keyframes float-3 {
  0%,
  100% {
    transform: translate(0, 0) rotate(0deg);
  }
  50% {
    transform: translate(-20px, -30px) rotate(3deg);
  }
}

.animate-spin {
  animation: spin 1s linear infinite;
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.logo-container:hover {
  transform: scale(1.05);
}

.logo-image {
  width: 28px;
  height: 28px;
}

.logo-text {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--primary-purple, #6b46c1);
}
</style>
